package com.atcc.test.controller;

import java.util.Currency;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.atcc.test.domain.Result;
import com.atcc.test.domain.ResultCode;
import com.atcc.test.pojo.BasicUser;
import com.atcc.test.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user/")
@Api("BasicUser增删改查接口")
public class UserController {
	
	@Autowired 
	private UserService userService;

	/**
	 * 存储单个用户
	 * @param basicUser
	 */
	@PostMapping("addBasicUser")
	@ApiOperation(value="增添User数据",notes="增添User数据")
	public void addBasicUser(@RequestBody BasicUser basicUser) {
		BasicUser data = basicUser;
		String currentUserName = data.getUserName();
		if(currentUserName!=null) {
				userService.saveBasicUser(data);
		}
	}
	
	/**
	 * 根据名字查询基础用户
	 * @param userName
	 * @return	BasicUser
	 */
	@GetMapping("queryBasciUserByName")
	@ApiOperation(value="根据名字查询User数据",notes="根据名字查询User数据")
	public Result<BasicUser> queryBasciUserByName(@RequestParam("userName") String userName){
		if(userName != null || userName != "") {
			BasicUser currentBasicUser 
				= userService.queryBasicUserByName(userName);
			if(currentBasicUser != null) {
				Result<BasicUser> result 
					= new Result<BasicUser>(ResultCode.SUCCESS,currentBasicUser);
				return result;
			}
		}
		Result<BasicUser> result =
				new Result<BasicUser>(ResultCode.PARAM_IS_BLANK,null);
		return result;
	}
	
	/**
	 * 根据用户ID查询单个用户BasicUsers
	 * @param userId
	 * @return
	 */
	@GetMapping("queryBasicUserByUserId")
	@ApiOperation(value="根据用户ID查询用户",notes="根据用户ID查询用户")
	public Result<BasicUser> queryBasicUserByUserId(@RequestParam("UserId") int userId)
	{
		if(userId != 0) {
			BasicUser currentUser 
				= userService.queryBasicUserByuserId(userId);
			if(currentUser != null) {
				Result<BasicUser> result 
					= new Result<BasicUser>(ResultCode.SUCCESS,currentUser);
				return result;
			}
		}
		Result<BasicUser> result 
			= new Result<BasicUser>(ResultCode.PARAM_IS_BLANK,null);
		return result;
	}
	
	/**
	 * 查询所有用户
	 * @return
	 */
	@GetMapping("queryAllBasicUser")
	@ApiOperation(value="查询所有User",notes="查询所有User")
	public Result<List<BasicUser>> queryAllBasicUser(){
		List<BasicUser> allBasicUser 
			= userService.queryAllBasicUser();
		Result<List<BasicUser>> result = 
				new Result<List<BasicUser>>(ResultCode.SUCCESS,allBasicUser);
		return result;
	}
	
	
}
